<!--
 * @Author: WangShuai 1830773767@qq.com
 * @Date: 2024-01-14 21:58:59
 * @LastEditors: WangShuai 1830773767@qq.com
 * @LastEditTime: 2025-06-18 12:39:46
 * @FilePath: \vue3_admin_template\project\src\views\product\sku\index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->

<template>
  <el-card>
    <el-table border :data="skuListArr" style="width: 100%">
      <el-table-column
        type="index"
        label="序号"
        width="80px"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="skuName"
        label="SKU名称"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="skuDesc"
        label="SKU描述"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column prop="skuDedaltImg" label="图片" align="center">
        <template #="{ row }">
          <img
            :src="row.skuDedaltImg"
            alt="暂无图片"
            style="width: 100px; height: 100px"
          />
        </template>
      </el-table-column>
      <el-table-column
        prop="weight"
        label="重量(KG)"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="price"
        label="价格(元)"
        align="center"
      ></el-table-column>
      <el-table-column fixed="right" label="操作" width="240px" align="center">
        <template #="{ row }">
          <el-button
            type="primary"
            size="small"
            :title="row.isSale == 1 ? '下架' : '上架'"
            :icon="row.isSale == 1 ? 'Bottom' : 'Top'"
            @click="skuSaleOrUnSale(row)"
            v-has="`btn.Sku.updown`"
          ></el-button>
          <el-button
            type="primary"
            size="small"
            title="编辑"
            @click="updateSku"
            v-has="`btn.Sku.update`"
          >
            <el-icon><Edit /></el-icon>
          </el-button>
          <el-button
            type="primary"
            size="small"
            title="查看"
            @click="findSku(row)"
            v-has="`btn.Sku.detail`"
          >
            <el-icon><InfoFilled /></el-icon>
          </el-button>
          <el-popconfirm
            class="box-item"
            :title="`确定要删除${row.skuName}么?`"
            placement="top-start"
            @confirm="removeSku(row)"
            @cancel="removeCancel"
            width="200px"
          >
            <template #reference>
              <el-button
                type="primary"
                size="small"
                title="删除"
                @click="removeSku(row)"
                v-has="`btn.Sku.remove`"
              >
                <el-icon><DeleteFilled /></el-icon>
              </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="margin: 10px 0"
      v-model:current-page="pageInfo.pageNo"
      v-model:page-size="pageInfo.limit"
      :page-sizes="[1, 3, 5, 7, 10]"
      size="small"
      :background="true"
      layout=" prev, pager, next, jumper,->,sizes,total,"
      :total="pageInfo.total"
      @size-change="handleSizeChange"
      @current-change="getHasSku"
    />
    <!-- 展示商品详情组件 -->
    <el-drawer v-model="drawer" direction="rtl">
      <template #header>
        <h4>查看商品详情</h4>
      </template>
      <template #default>
        <el-row :gutter="10" style="margin: 10px 0">
          <el-col :span="6">名称</el-col>
          <el-col :span="18">{{ skuInfo.skuName }}</el-col>
        </el-row>
        <el-row :gutter="10" style="margin: 10px 0">
          <el-col :span="6">描述</el-col>
          <el-col :span="18">{{ skuInfo.skuDSc }}</el-col>
        </el-row>
        <el-row :gutter="10" style="margin: 10px 0">
          <el-col :span="6">价格</el-col>
          <el-col :span="18">{{ skuInfo.price }}</el-col>
        </el-row>
        <el-row :gutter="10" style="margin: 10px 0">
          <el-col :span="6">平台属性</el-col>
          <el-col :span="18">
            <el-tag
              type="success"
              v-for="item in skuInfo.skuAttrValueList"
              :key="item.id"
              style="margin: 5px"
            >
              {{ item.valueName }}
            </el-tag>
          </el-col>
        </el-row>
        <el-row :gutter="10" style="margin: 10px 0">
          <el-col :span="6">销售属性</el-col>
          <el-col :span="18">
            <el-tag
              type="success"
              v-for="item in skuInfo.skuSaleAttrValueList"
              :key="item.id"
              style="margin: 5px"
            >
              {{ item.saleAttrValueName }}
            </el-tag>
          </el-col>
        </el-row>
        <el-row :gutter="10" style="margin: 10px 0">
          <el-col :span="6">商品图片</el-col>
          <el-col :span="18">
            <el-carousel :interval="4000" type="card" height="200px">
              <el-carousel-item
                v-for="item in skuInfo.skuImageList"
                :key="item.id"
              >
                <img
                  :src="item.imgUrl"
                  alt=""
                  style="width: 100%; height: 100%"
                  crossorigin="anonymous"
                />
              </el-carousel-item>
            </el-carousel>
          </el-col>
        </el-row>
      </template>
    </el-drawer>
  </el-card>
</template>

<script setup lang="ts">
import { reactive, onMounted, ref } from 'vue'
import {
  reqSkuList,
  reqSkuOnSale,
  reqSkuCanelSale,
  reqSkuInfo,
  reqRemoveSku,
} from '@/api/product/sku'
import type {
  SkuResponseData,
  Records,
  SkuData,
  SkuInfoData,
} from '@/api/product/sku/type'
import { ElMessage } from 'element-plus'
let pageInfo = reactive({
  pageNo: 1,
  limit: 10,
  total: 0,
})
let skuListArr = reactive<Records>([])
let drawer = ref(false) //展示抽屉显示和隐藏
let skuInfo = ref<any>({}) //保存skuInfo对象
onMounted(() => {
  getHasSku()
})
//获取已有的sku列表
const getHasSku = async (pager = 1) => {
  pageInfo.pageNo = pager
  let result: SkuResponseData = await reqSkuList(
    pageInfo.pageNo,
    pageInfo.limit,
  )
  if (result.code == 200) {
    skuListArr = result.data.records
    pageInfo.total = result.data.total
  }
}
//改变每页条数
const handleSizeChange = (value: number) => {
  pageInfo.limit = value
}
//已有的商品sku上架或下架方法
const skuSaleOrUnSale = async (row: SkuData) => {
  let result: any =
    row.isSale == 1
      ? await reqSkuCanelSale(row.id as number)
      : await reqSkuOnSale(row.id as number)
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: `商品${row.isSale == 1 ? '下架' : '上架'}成功!`,
    })
    getHasSku(pageInfo.pageNo == 1 ? 1 : pageInfo.pageNo)
  } else {
    ElMessage({
      type: 'error',
      message: `商品${row.isSale == 1 ? '下架' : '上架'}失败!`,
    })
  }
}
const updateSku = () => {
  ElMessage({
    type: 'success',
    message: `正在开发中!`,
  })
}
//查看商品详情回调
const findSku = async (row: SkuData) => {
  drawer.value = true
  let result: SkuInfoData = await reqSkuInfo(row.id as number)
  if (result.code == 200) {
    skuInfo.value = result.data
  }
}
//删除sku方法
const removeSku = async (row: SkuData) => {
  let result = await reqRemoveSku(row.id as number)
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: `删除成功`,
    })
    getHasSku(skuListArr.length > 1 ? pageInfo.pageNo : pageInfo.pageNo - 1)
  } else {
    ElMessage({
      type: 'error',
      message: `删除失败`,
    })
  }
}
const removeCancel = () => {
  ElMessage({
    type: 'success',
    message: `删除取消`,
  })
}
</script>

<style lang="scss" scoped>
.el-carousel__item h3 {
  color: #475669;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>
